package com.sf.service.impl;

import com.google.common.collect.Maps;
import com.sf.manager.CollectInfoManager;
import com.sf.manager.ProMarketBaseManager;
import com.sf.model.CollectInfo;
import com.sf.model.CollectInfoExample;
import com.sf.model.ProMarketBase;
import com.sf.model.ProMarketBaseExample;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class InitMarketJob {
    private static final Logger logger = LoggerFactory.getLogger(InitMarketJob.class);
    @Autowired
    private CollectInfoManager collectInfoManager;
    @Autowired
    private ProMarketBaseManager proMarketBaseManager;
    private static Map<String, Boolean> initMaps = Maps.newConcurrentMap();

    public void doBiz() {


        //查询所有的专业市场信息
        List<ProMarketBase> proMarketBases = proMarketBaseManager.selectByExample(new ProMarketBaseExample());
        if (CollectionUtils.isEmpty(proMarketBases)) {
            logger.error("proMarketBases is empty");
            return;
        }
        //循环处理所有的专业市场集货团信息
        for (ProMarketBase proMarketBase : proMarketBases) {
            if (initMaps.get(proMarketBase.getMktId()) == null) {
                processMarketBase(proMarketBase);
                initMaps.put(proMarketBase.getMktId(), Boolean.TRUE);
            }
        }

    }

    private void processMarketBase(ProMarketBase proMarketBase) {
        //判断是否有未完成的集货团
        CollectInfoExample example = new CollectInfoExample();
        example.createCriteria().andMktIdEqualTo(proMarketBase.getMktId());
        if (collectInfoManager.countByExample(example) == 0) {
            CollectInfo collectInfo = new CollectInfo();
            collectInfo.setUserdef1("1");
            collectInfoManager.insert(collectInfo, proMarketBase);
        }

    }


}
